﻿/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_MEM_QUEUE_H
#define MK_MEM_QUEUE_H
#include "MkLock.h"
#if(!defined(WIN32) || defined(_WIN32_WCE)) && !defined(HAVE_PTHREAD_WIN32)
#include <pthread.h>
#include <time.h>
#define  BOOL int
#define  TRUE 1
#define  FALSE  0
typedef  void  *HANDLE;
#define WAIT_OBJECT_0 0
#define WAIT_TIMEOUT ETIMEDOUT
typedef struct
{
    pthread_cond_t  cond_t;
    pthread_mutex_t  mutex_t;
} EVENTHANDLE;

void *CreateEvent(char *p1, BOOL b1, BOOL b2, char *p2);
int WaitForSingleObject(void *event_handle, unsigned long millseconds);
void SetEvent(void *event_handle);
void CloseHandle(void *event_handle);
#endif

class MkApi CMkMemQueue
{
public:
    ~CMkMemQueue();

    static CMkMemQueue *CreateNew(Uint32 MemPoolSize);

    Uint32 Input(Uint8 *pbuf, Uint32 nLen, Uint32 WaitTime = 0);
    Uint32 OutPut(OUT Uint8 *pbuf, INOUT Uint32 *pLen, BOOL bRecvAll = TRUE, Uint32 WaitTime = 0);
    void Clear();
    void Realse();
protected:
private:
    CMkMemQueue();
private:
    Uint8          *m_buf;
    Uint32          m_PoolSize;
    Uint32          m_ProductPos;
    Uint32          m_ConsumerPos;
    HANDLE          m_InEvent;
    HANDLE          m_OutEvent;
    CMkLock         m_Lock;
    BOOL            m_bRelease;
};








#endif
